package com.uchengwang.ui.cm_ui.me.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserBean;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.SwitchAccountUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/7.
 * 转让社团管理员
 */

public class TransferClubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<UserBean> list = new ArrayList<>();
    private int type;//0查看社团成员  1转让社团管理权 2邀请社团成员加入默认创建的社团群
    private String code;//转移社团管理权需要验证码
    private List<RyUserBean> memmbers;

    public TransferClubAdapter(Context context, int type){
        this.context = context;
        this.type = type;
    }

    public void setData(List<UserBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<UserBean> getData(){
        return list;
    }

    public void setCode(String code){
        this.code = code;
    }

    /**
     * 设置群成员
     * */
    public void setMembers(List<RyUserBean> members){
        this.memmbers = members;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.apply_item, parent, false);
        return new TransferVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        final TransferVH holder = (TransferVH) h;

        final UserBean bean = list.get(position);

        String headUrl = bean.getUserAvatar();
        if (TextUtils.isEmpty(headUrl)){
            ImageManager.getInstance().loadImage(context, holder.apply_image, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.apply_image, headUrl, ImageManager.ImageType.CIRCLE);
        }

        String remark = bean.getRemarkName();
        if (TextUtils.isEmpty(remark)){
            holder.apply_name.setText(bean.getNickName());
        }else {
            holder.apply_name.setText(remark);
        }

        holder.apply_info.setText(bean.getUserSign());

        if (position ==0){
            //当前用户和社长账号相同
            holder.apply_type.setVisibility(View.VISIBLE);
            holder.apply_type.setText("社长");
        }

        switch (type){
            case 2:
                holder.apply_ln.setVisibility(View.VISIBLE);
                boolean isMem = isMember(bean.getId());
                if (isMem){
                    holder.apply_checkBox.setVisibility(View.INVISIBLE);
                }else {
                    holder.apply_checkBox.setVisibility(View.VISIBLE);
                }

                holder.apply_checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        boolean isCheck = bean.isCheck();
                        holder.apply_checkBox.setChecked(!isCheck);
                        bean.setCheck(!isCheck);
                    }
                });

                break;
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 0:
                        UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getId());
                        break;
                    case 1:
                        if (position !=0){
                            //0是社长
                            transfer(bean.getId(), bean.getNickName());
                        }else {
                            UCommonUtil.getUtil().showToast(context, "已经是社长了-_-");
                        }
                        break;
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }



    class TransferVH extends RecyclerView.ViewHolder {

        private View itemView;
        private ImageView apply_image;
        private TextView apply_name, apply_info, apply_type;
        private AppCompatCheckBox apply_checkBox;
        private LinearLayout apply_ln;


        public TransferVH(View view) {
            super(view);
            AutoUtils.autoSize(view);
            itemView = view;
            apply_image = (ImageView) view.findViewById(R.id.apply_image);
            apply_name = (TextView) view.findViewById(R.id.apply_name);
            apply_info = (TextView) view.findViewById(R.id.apply_info);
            apply_type = (TextView) view.findViewById(R.id.apply_type);
            apply_ln = (LinearLayout) view.findViewById(R.id.apply_ln);

            apply_checkBox = (AppCompatCheckBox) view.findViewById(R.id.apply_checkBox);

        }
    }

    /**
     * 转移社团管理权
     * */
    private void transfer(final long uid, String name){
        DialogUtils.getUtils().showDialogSingle(context, "确定将社团管理员转移给" + name + "?", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.getUtils().cancel();
                LoadingUtils.getUtils().loading(context);
                MapRequest map = new MapRequest();
                map.put(BaseConfig.CODE, code);
                map.put(BaseConfig.RECEIVE_UID, uid);
                HttpManager.newInstance((Activity) context).query(ClubConfig.Transfer.method_put, ClubConfig.Transfer.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        Log.d("jjf", "stutas="+status+"=msg="+msg);
                        LoadingUtils.getUtils().hide();
                        UCommonUtil.getUtil().showToast(context, "转让失败");
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        Log.d("jjf", "stutas="+status+"=msg="+msg);

                        if (status ==0){
                            SwitchAccountUtils.getUtils().transferClubexit(context);
                        }else {
                            LoadingUtils.getUtils().hide();
                            UCommonUtil.getUtil().showToast(context, "转让失败");
                        }
                    }
                });



            }
        });
    }

    /**
     * 判断是不是群成员
     * */
    public boolean isMember(long uid){
        if (memmbers == null || memmbers.size() <1){
            return false;
        }
        for (RyUserBean bean : memmbers){
            if (bean.getMemberUid() == uid){
                return true;
            }
        }
        return false;
    }

}
